<template>
  <!-- 结束就诊 -->
  <el-dialog title="结束就诊" :visible="value" width="400px" custom-class="dialog-box" @close="onClose" @open="onOpen" :close-on-click-modal="false" :append-to-body="true">
    <el-form ref="form" :model="form" :rules="rules" label-position="top">
      <el-form-item label="本次诊疗费：" prop="cureFeeId" :rules="isvalid?null:cureFeeIdRules">
        <el-select v-model="form.cureFeeId" :disabled="isvalid">
          <el-option v-if="isvalid" label="免费（￥0）" value="" ></el-option>
          <el-option v-for="(item,index) in cureFeeArr" :key="index" :label="item.registerName+'(￥'+item.registerMoney+')'" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "cureId", "doctorId", "deptId"],
  computed: {
    templateData() {
      return this.$store.state.doctorMZ.templateData;
    }
  },
  data() {
    return {
      btnLoading: false,
      isvalid: false,
      form: {
        cureId: this.cureId,
        cureFeeId: ""
      },
      rules: {},
      cureFeeIdRules: [
        { required: true, message: "此项必填", trigger: "change" }
      ],
      cureFeeArr: []
    };
  },
  mounted() {
    let th = this;
  },
  methods: {
    onSubmit() {
      let th = this;
      let fn = () => {
        th.btnLoading = true;
        th.$api
          .closeCure(th.form)
          .then(res => {
            if (res.executed) {
              th.$message.success("操作成功");
              th.$emit("cureEnd");
              th.onClose();
            } else {
              th.$message.error(res.message);
            }
            th.btnLoading = false;
          })
          .catch(() => {
            th.btnLoading = false;
          });
      };
      //套餐结束
      const fn2 = () => {
        let j = {
          cureId: th.form.cureId,
          cureFeeId: th.form.cureFeeId,
          userPackageItemId: th.templateData.id
        };
        th.btnLoading = true;
        th.$api
          .closeCureCombo(j)
          .then(res => {
            if (res.executed) {
              th.$message.success("操作成功");
              th.$emit("cureEnd");
              th.onClose();
            } else {
              th.$message.error(res.message);
            }
            th.btnLoading = false;
          })
          .catch(() => {
            th.btnLoading = false;
          });
      };
      th.$refs["form"].validate(v => {
        if (v) {
          if (th.templateData.id) {
            fn2();
          } else {
            fn();
          }
        }
      });
    },
    onOpen() {
      let th = this;
      let j = {
        pageNum: 1,
        pageSize: 100,
        hpId: th.adminUser.hpId,
        status: 1,
        // deptId: th.deptId,
        doctorId: th.doctorId
      };
      th.$api.medicalExpensepageQuery(j).then(res => {
        th.cureFeeArr = res.dataBody.list;
      });
      th.$api.preCureRegisterOrder({ cureId: th.cureId }).then(res => {
        if (res.executed) {
          th.isvalid = res.data;
        }
      });
    },
    onClose() {
      let th = this;
      th.$emit("input", false);
    }
  }
};
</script>
